﻿using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using SqlSugar;
using IM.Easy.Core.Attributes;
using IM.Easy.SystemServices.Dtos.Versions;
using Mapster;
using IM.Easy.Core.Utils;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// ReleaseLogController 控制器
    /// </summary>
    public class ReleaseLogController : ControllerBase
    {
        private readonly IReleaseLogService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public ReleaseLogController(IReleaseLogService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有ReleaseLog对象
        /// </summary>
        /// <returns>返回ReleaseLog列表</returns>
        [HttpGet]
        [Api(Name = "获取版本列表", AllowAnonymous = true, Required = true)]
        public PageResponse<ReleaseLogDto> GetList([FromQuery] ReleaseLogQueryDto query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 根据ID获取ReleaseLog对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的ReleaseLog对象</returns>
        [HttpGet("{id}")]
        [Api(Name = "获取版本信息", Perm = "sys:releaselog:info")]
        public ReleaseLogDto GetById([FromRoute] long id)
        {
            var result = _service.GetById(id);
            var dto = result.Adapt<ReleaseLogDto>();
            return dto ?? throw new KeyNotFoundException($"未找到ID为 {id} 的ReleaseLog对象。");
        }

        /// <summary>
        /// 暂存
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Api(Name = "暂存", Perm = "sys:releaselog:stage")]
        [HttpPost]
        public ReleaseLog Stage([FromBody] ReleaseLogDto dto)
        {
            dto.Status = 0;
            if (dto.Id == default) dto.Id = SnowFlakeSingle.instance.NextId();
            var model = dto.Adapt<ReleaseLog>();
            if (_service.VersionEnable(model))
            {
                _service.InsertOrUpdate(model);
            }
            else
            {
                ExceptionUtils.ThrowBusinessException($"版本号 {dto.VersionCode} 已存在，请使用其他版本号。");
            }
            return model;
        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <returns></returns>
        [Api(Name = "发布", Perm = "sys:releaselog:publish")]
        [HttpPost]
        public ReleaseLog Release([FromBody] ReleaseLogDto dto)
        {
            dto.Status = 1;
            if (dto.Id == default) dto.Id = SnowFlakeSingle.instance.NextId();
            var model = dto.Adapt<ReleaseLog>();
            model.ReleaseDate = DateTime.Now;
            if (_service.VersionEnable(model))
            {
                _service.InsertOrUpdate(model);
            }
            else
            {
                ExceptionUtils.ThrowBusinessException($"版本号 {dto.VersionCode} 已存在，请使用其他版本号。");
            }
            return model;
        }

        /// <summary>
        /// 撤销发布
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="KeyNotFoundException"></exception>
        [Api(Name = "撤销发布", Perm = "sys:releaselog:revoke")]
        [HttpPut("revoke/{id}")]
        public bool Revoke([FromRoute] long id)
        {
            var entity = _service.GetById(id) ?? throw new KeyNotFoundException($"未找到ID为 {id} 的ReleaseLog对象。");
            entity.Status = 0;
            entity.ReleaseDate = null;
            return _service.Update(entity);
        }

        /// <summary>
        /// 批量删除ReleaseLog对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        [Api(Name = "删除版本信息", Perm = "sys:releaselog:del")]
        public void Delete([FromBody] long[] ids)
        {
            _service.Delete(ids);
        }

        /// <summary>
        /// 获取最新版本信息
        /// </summary>
        /// <returns></returns>
        [Api(Name = "获取当前版本", AllowAnonymous = true, Required = true)]
        public ReleaseLogDto GetLatest()
        {
            var result = _service.GetLatest();
            var dto = result.Adapt<ReleaseLogDto>();
            return dto;
        }
    }
}
